//
//  STPersonalCollectionViewCell.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/11/26.
//

import UIKit
import SDWebImage

protocol STPersonaChangeImageDelegate: NSObjectProtocol  {
    func handleChangeImageAction(indexPath: IndexPath) ->Void
}

class STPersonalCollectionViewCell: UICollectionViewCell {
    
    class var identifier: String {
        get {
            return "STPersonalCollectionViewCell"
        }
    }
    
    class func registerTo(_ collectionView: UICollectionView) {
        collectionView.register(STPersonalCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: STPersonalCollectionViewCell.identifier)
    }
    
    class func reusableCell(_ collectionView: UICollectionView, indexPath: IndexPath) -> STPersonalCollectionViewCell? {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: STPersonalCollectionViewCell.identifier, for: indexPath) as? STPersonalCollectionViewCell
        return cell
    }
    
    weak var delegate: STPersonaChangeImageDelegate?
    var indexPath: IndexPath = IndexPath.init()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.white
        layer.cornerRadius = 12
        
        addSubview(descLabel)
        descLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.left.equalTo(16)
            make.height.equalTo(22)
        }
        
        addSubview(iconBtn)
        iconBtn.snp.makeConstraints { (make) in
            make.right.equalTo(-16)
            make.centerY.equalTo(descLabel)
            make.width.equalTo(66)
            make.height.equalTo(66)
        }
        
        addSubview(rightLabel)
        rightLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(descLabel)
            make.right.equalTo(-16)
            make.height.equalTo(22)
        }
    
        addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.bottom.equalTo(0)
            make.height.equalTo(1)
        }
        
        iconBtn.addTarget(self, action: #selector(handleChangeUserToXiangAction), for: .touchUpInside)
    }
    
    @objc func handleChangeUserToXiangAction() {
        delegate?.handleChangeImageAction(indexPath: self.indexPath)
    }
    
    public func updateCellData(title: String, value: Any?, _ index: Int) {
        descLabel.text = title
        if(index == 0) {
            iconBtn.isHidden = false
            rightLabel.isHidden = true
            let avatar = value as? String
            if(avatar != nil) {
                let realUrl = URL.init(string: "\(kServiceHost)\(avatar ?? "")")
                iconBtn.sd_setImage(with: realUrl, for: UIControl.State.normal, placeholderImage: UIImage.init(named: "icon_touxiang"), options: [.retryFailed])
            }
        } else {
            iconBtn.isHidden = true
            rightLabel.isHidden = false
            rightLabel.text = value as? String
        }
        line.isHidden = index == 3
    }
    
    lazy var iconBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setImage(UIImage.init(named: "icon_touxiang"), for: .normal)
        btn.layer.cornerRadius = 33
        btn.layer.masksToBounds = true
        return btn
    }()
    
    
    lazy var descLabel: UILabel = {
        let lb = UILabel.init()
        lb.textAlignment = .center
        return lb
    }()
    
    lazy var rightLabel: UILabel = {
        let lb = UILabel.init()
        lb.textAlignment = .center
        return lb
    }()
    
    lazy var line: UIView = {
        let v = UIView.init()
        v.backgroundColor = UIColor.init(hexString: "#F7F7F7", alpha: 1)
        return v
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
